exports.createToken = function(req,res){

    var bodyParams = req.routeInfo.bodyParams;

    //是否已有token
    var isToken = false;

    //保存token信息
    var tokenData = {};

    //返回结果
    var resultData = {};

    //查询用户是否已有令牌
    function searchUserToken(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url    : CORE_APIConfig.coreAPI.easydCloudUserTokenSearch.Request_Api_Url + "/cut_username/" + bodyParams.username + "/cut_app_key/" +bodyParams.appKey + "/cut_is_delete/0",  
                method : 'GET'
            },
            function (data) {

                if(data.result.data.length > 0){
                   tokenData = data.result.data[0];
                   isToken = true
                }

                cb();

            }
        )
    }

    //有则更新无则新增操作
    function updateOrAddToken(cb){

        //更新
        if(isToken){

            //查看token是否过期
            var overdueTime = tokenData.cut_overdue_time;

            //对比查看是否过期
            var nowTime = common.nowTime(false);

            //已过期
            if(nowTime > overdueTime) {

                //生成新的token
                var userToken = common.createUserAppToken();

                //延后token的过期时间
                var newTokenTime = nowTime + config.serverAuth.tokenOverdueTime;

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url    : CORE_APIConfig.coreAPI.easydCloudUserTokenUpdate.Request_Api_Url,  
                        method : 'POST',
                        data   : {
                            update_where : 'cut_username,cut_app_key',
                            cut_app_key  : bodyParams.appKey,
                            cut_username : bodyParams.username,
                            cut_token    : userToken,
                            cut_overdue_time : newTokenTime
                        }
                    },
                    function (data) {
                        
                        resultData.token = userToken;
                    
                        cb();
        
                    }
                )
                
            //未过期
            }else{

                resultData.token = tokenData.cut_token;

                cb();

            }



        //新增    
        }else{

            //生成token
            var userToken = common.createUserAppToken();

            var params = {
                cut_username : bodyParams.username,
                cut_token    : userToken,
                cut_overdue_time : common.nowTime(false) + config.serverAuth.tokenOverdueTime,
                cut_app_key  : bodyParams.appKey
            }

            R.SEND_HTTP(
                req,
                res,
                {
                    url    : CORE_APIConfig.coreAPI.easydCloudUserTokenAdd.Request_Api_Url,  
                    method : 'POST',
                    data   : params
                },
                function (data) {

                    resultData.token = userToken;

                    cb();
    
                }
            )

        }
    }

    F.Serial([
        searchUserToken,
        updateOrAddToken
    ],() => {
        RES.successResponse(res,resultData);
    })


}